package com.gxa.service.investment.impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxa.dto.investmentDto.CompanyQuitResult;
import com.gxa.dto.investmentDto.PlanDto;
import com.gxa.entity.investment.Plan;
import com.gxa.mapper.investment.PlanMapper;
import com.gxa.service.investment.PlanService;
import com.gxa.vo.investmentVo.PlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yjl
 * @since 2022-10-12 11:37:51
 */
@Service
public class PlanServiceImpl extends ServiceImpl<PlanMapper, Plan> implements PlanService {

    @Autowired
    private PlanMapper planMapper;
    @Override
    public int add(Plan plan) {
        return planMapper.insert(plan);
    }

    @Override
    public int delete(int id) {
        return planMapper.deleteById(id);
    }

    @Override
    public PageInfo<PlanVo> queryByPlanDto(Integer pageNum, Integer pageSize, PlanDto planDto) {
        Page<PlanVo> page = PageHelper.startPage(pageNum,pageSize);
        List<PlanVo> companyQuitResultList = this.planMapper.queryByPlanDto(planDto);
        //计算已完成占比

        for (PlanVo planVo:companyQuitResultList) {
            int b = planVo.getActuallyComplete();
            int a = planVo.getRemainingQuantity();
            int d = a+b;
            planVo.setPlanComplete(d);
            Float c = (float) a/b;
            DecimalFormat df = new DecimalFormat("0.00%");
            String s = df.format(c);
            planVo.setActuallyPercentage(s);
        }

        PageInfo<PlanVo> pageInfo = new PageInfo<>(companyQuitResultList);

        return pageInfo;
    }

    @Override
    public int update(Plan plan) {
        return planMapper.updateById(plan);
    }
}
